package com.bsoft.ecommerce.filter;

import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.context.annotation.EnableMBeanExport;
import org.springframework.core.Ordered;
import org.springframework.http.HttpStatus;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

/**
 * @program: e-commerce-springclound
 * @description:
 * @author: hdh
 * @create: 2022:06:23 10:22
 */

// http 请求头部携带token 验证过滤器
public class HeaderTokenGatewayFilter implements GatewayFilter , Ordered {

    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
         // 从http Header
        String token = exchange.getRequest().getHeaders().getFirst("token");
        if("imooc".equals(token)){
            return  chain.filter(exchange);
        }
        // 标记此次请求没有权限 并结束这次请求
        exchange.getResponse().setStatusCode(HttpStatus.UNAUTHORIZED);
        return exchange.getResponse().setComplete();
    }

    @Override
    public int getOrder() {
        return HIGHEST_PRECEDENCE+2;

    }
}
